package app.stock.client;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import app.stock.common.Constants;
import app.stock.common.Transaction;
import app.stock.common.TransactionTypes;
import app.stock.server.StockManager;

public class TransactionListPanel extends JPanel {
    private JPanel jPanel1;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JComboBox jComboBox1;
    private JComboBox jComboBox2;
    private JButton jButton1;
    private JPanel jPanel2;
    private JScrollPane jScrollPane1;
    private DataTable jTable1;
    private JPanel jPanel3;
    private JButton jButton2;
    private JButton jButton3;
    StockManager sm; // @jve:decl-index=0:

    public TransactionListPanel() {
        super();
        initialize();
    }

    private void initialize() {
        sm = new StockManager();
        List txList = sm.getTransactionList(null);
        BorderLayout jPanel1Layout = new BorderLayout();
        this.setLayout(jPanel1Layout);
        {
            jPanel1 = new JPanel();
            jLabel1 = new JLabel();
            jPanel1.add(jLabel1);
            jLabel1.setText(Transaction.typeLabel);
            Vector jComboBox1ModelData = new Vector();
            jComboBox1ModelData.add(Constants.label_all);
            jComboBox1ModelData.addAll(Arrays.asList(TransactionTypes.values));
            ComboBoxModel jComboBox1Model = new DefaultComboBoxModel(
                    jComboBox1ModelData);
            jComboBox1 = new JComboBox();
            jPanel1.add(jComboBox1);
            jComboBox1.setModel(jComboBox1Model);
            jLabel3 = new JLabel();
            jPanel1.add(jLabel3);
            jLabel3.setText("        ");
            jLabel2 = new JLabel();
            jPanel1.add(jLabel2);
            jLabel2.setText(Transaction.codeLabel);
            Vector jComboBox2ModelData = new Vector();
            jComboBox2ModelData.add(Constants.label_all);
            jComboBox2ModelData.addAll(sm.getTransactonMapGroupedByStock(
                    txList, null).keySet());
            ComboBoxModel jComboBox2Model = new DefaultComboBoxModel(
                    jComboBox2ModelData);
            jComboBox2 = new JComboBox();
            jPanel1.add(jComboBox2);
            jComboBox2.setModel(jComboBox2Model);
            jLabel4 = new JLabel();
            jPanel1.add(jLabel4);
            jLabel4.setText("                    ");
            jButton1 = new JButton();
            jPanel1.add(jButton1);
            jButton1.setText(Constants.label_operationQuery);
            jButton1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButton1MouseClicked(evt);
                }
            });
            this.add(jPanel1, BorderLayout.NORTH);
        }
        {
            jPanel2 = new JPanel();
            BorderLayout jPanel12Layout = new BorderLayout();
            jPanel2.setLayout(jPanel12Layout);
            jTable1 = new DataTable();
            this.setTableData(txList);
            jTable1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jTable1MouseClicked(evt);
                }
            });
            jScrollPane1 = new JScrollPane();
            jScrollPane1.setViewportView(jTable1);
            jPanel2.add(jScrollPane1, BorderLayout.CENTER);
            this.add(jPanel2, BorderLayout.CENTER);
        }
        {
            jPanel3 = new JPanel();
            jButton2 = new JButton();
            jPanel3.add(jButton2);
            jButton2.setText(Constants.label_operationAdd);
            jButton2.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButton2MouseClicked(evt);
                }
            });
            jButton3 = new JButton();
            jPanel3.add(jButton3);
            jButton3.setText(Constants.label_operationDelete);
            jButton3.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                    jButton3MouseClicked(evt);
                }
            });
            this.add(jPanel3, BorderLayout.SOUTH);
        }
    }

    private void setTableData(List txList) {
        Object[] tableHeaderData = new Object[] { Constants.label_ID,
                Transaction.dateLabel, Transaction.typeLabel,
                Transaction.codeLabel, Transaction.nameLabel,
                Transaction.priceLabel, Transaction.amountLabel,
                Transaction.sum1Label, Transaction.shouxufeiLabel,
                Transaction.yinhuashuiLabel, Transaction.guohufeiLabel,
                Transaction.sum2Label, Transaction.currentCashLabel };
        Object[][] tableData = new Object[txList.size()][tableHeaderData.length];
        for (int i = 0; i < txList.size(); i++) {
            Transaction tx = (Transaction) txList.get(i);
            tableData[i] = new Object[] { tx.id, tx.date, tx.type, tx.code,
                    tx.name, tx.price, tx.amount, tx.sum1, tx.shouxufei,
                    tx.yinhuashui, tx.guohufei, tx.sum2, tx.currentCash };
        }
        this.jTable1.setTableData(tableHeaderData, tableData);
        this.jTable1.initialize();
    }

    private void jButton1MouseClicked(MouseEvent evt) {
        System.out.println("jButton1.mouseClicked, event=" + evt);
        Transaction txCondition = new Transaction();
        txCondition.type = this.jComboBox1.getSelectedItem().toString();
        txCondition.code = this.jComboBox2.getSelectedItem().toString();
        List txList = sm.getTransactionList(txCondition);
        this.setTableData(txList);
    }

    private void jButton2MouseClicked(MouseEvent evt) {
        System.out.println("jButton2.mouseClicked, event=" + evt);
        JDialog dialog = new TransactionPanelAdd(
                StockManagerClient.getStockManagerClient());
        dialog.setSize(300, 500);
        dialog.setVisible(true);
    }

    private void jButton3MouseClicked(MouseEvent evt) {
        System.out.println("jButton3.mouseClicked, event=" + evt);
        int rows = this.jTable1.getRowCount();
        for (int i = 0; i < rows; i++) {
            Boolean selected = (Boolean) this.jTable1.getValueAt(i, 1);
            if (selected) {
                Transaction tx = new Transaction();
                tx.id = (Integer) this.jTable1.getValueAt(i, 2);
                sm.deleteTransaction(tx);
            }
        }
    }

    private void jTable1MouseClicked(MouseEvent evt) {
        System.out.println("jTable1.mouseClicked, event=" + evt.getSource());
        if (this.jTable1.getSelectedColumn() == this.jTable1.getColumnCount() - 2) {
            Transaction tx = new Transaction();
            int row = this.jTable1.getSelectedRow();
            tx.id = (Integer) this.jTable1.getValueAt(row, 2);
            tx.date = (Date) this.jTable1.getValueAt(row, 3);
            tx.type = (String) this.jTable1.getValueAt(row, 4);
            tx.code = (String) this.jTable1.getValueAt(row, 5);
            tx.name = (String) this.jTable1.getValueAt(row, 6);
            tx.price = (Float) this.jTable1.getValueAt(row, 7);
            tx.amount = (Integer) this.jTable1.getValueAt(row, 8);
            tx.sum1 = (Float) this.jTable1.getValueAt(row, 9);
            tx.shouxufei = (Float) this.jTable1.getValueAt(row, 10);
            tx.yinhuashui = (Float) this.jTable1.getValueAt(row, 11);
            tx.guohufei = (Float) this.jTable1.getValueAt(row, 12);
            tx.sum2 = (Float) this.jTable1.getValueAt(row, 13);
            JDialog dialog = new TransactionPanelEdit(
                    StockManagerClient.getStockManagerClient(), tx);
            dialog.setSize(300, 500);
            dialog.setVisible(true);
        } else if (this.jTable1.getSelectedColumn() == this.jTable1
                .getColumnCount() - 2) {
            Transaction tx = new Transaction();
            tx.id = (Integer) this.jTable1.getValueAt(
                    this.jTable1.getSelectedRow(), 2);
            // sm.deleteTransaction(tx);
        }
    }
} // @jve:decl-index=0:visual-constraint="28,-3"
